import { Point } from '@/env';
import { defaultErrorHandler } from '@/unit/error-handler';
import gcoordWGS84ToBD09 from '@/unit/gcoord';
import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';
interface LocationMainState {
  lng: number;
  lat: number;
  timer: number;
}
export const useLocationMainStore = defineStore({
  id: 'LocationMainStore',
  state: (): LocationMainState => ({
    lng: 116.28019,
    lat: 40.049191,
    timer: 0,
  }),
  actions: {
    /**
     * 获取定位
     */
    async getLoaction() {
      if (window.navigator.geolocation) {
        // 116.35520952594115 39.9974382312823
        const getSuccess = async (infor: GeolocationPosition) => {
          const result = gcoordWGS84ToBD09(
            infor.coords.longitude,
            infor.coords.latitude,
          );
          console.log(result);
          this.setLng(result[0]);
          this.setLat(result[1]);
        };

        const getError = async (infor: GeolocationPositionError) => {
          console.log(infor);
          defaultErrorHandler(infor.message);
        };
        window.navigator.geolocation.getCurrentPosition(getSuccess, getError);
      } else {
        message.warn('不支持定位！');
      }
    },

    /**
     * 设置经度
     * @param value
     */
    setLng(value: number) {
      this.lng = value;
    },

    /**
     * 设置维度
     * @param value
     */
    setLat(value: number) {
      this.lat = value;
    },

    setTimer(value: number) {
      this.timer = value;
    },
  },
});
